﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 转盘
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int ItemNum = 4;
        private List<String> lstItem=new();
        private Storyboard myStoryBoard = new();
        private RotateTransform canRotate = new();
        private DoubleAnimation rotateAnimation = new();


        public MainWindow()
        {
            InitializeComponent();

            lstItem.Add("一号食堂");
            lstItem.Add("二号食堂");
            lstItem.Add("三号食堂");
            lstItem.Add("四号食堂");

            GeometryGroup DrawBoard = new();
            
            double dAngle = (2*Math.PI) / (double)ItemNum;
            for(int i=0;i<ItemNum;i++)
            {
                DrawBoard.Children.Add(new LineGeometry(new Point(150, 150), new Point(150+Math.Sin(dAngle * i)*130, 150+Math.Cos(dAngle * i)*130)));
            }
            DrawBoard.Children.Add(new EllipseGeometry(new Point(150, 150), 130, 130));
            GeometryDrawing aGD = new();
            aGD.Geometry = DrawBoard;
            aGD.Brush = new LinearGradientBrush(Colors.White, Colors.White, 0);
            aGD.Pen = new Pen(Brushes.Black, 3);
            DrawingImage drawingImage = new(aGD);
            canDraw.Source = drawingImage;
            canDraw.Stretch = Stretch.None;

            int iCount = 0;
            foreach(String x in lstItem)
            {
                VisualBrush textBrush1 = new();
                TextBlock txtItem1 = new();
                txtItem1.Text = x;
                txtItem1.FontSize = 15;
                textBrush1.Visual = txtItem1;
                Rectangle rctItem1 = new();
                rctItem1.Width = 80;
                rctItem1.Height = 30;
                rctItem1.Fill = textBrush1;
                rctItem1.SetValue(Canvas.ZIndexProperty, 1);
                rctItem1.SetValue(Canvas.LeftProperty, Math.Sin(iCount*dAngle+dAngle/2)*80+150-40);
                rctItem1.SetValue(Canvas.TopProperty, Math.Cos(iCount*dAngle+dAngle/2)*80+150-15);
                rctItem1.RenderTransform = new RotateTransform((-iCount * dAngle - dAngle / 2)/(2*Math.PI)*360,40,15);
                canMainBoard.Children.Add(rctItem1);
                iCount++;
            }

            //Animation Part
            canMainBoard.RenderTransform = canRotate;
            canRotate.CenterX = 150;
            canRotate.CenterY = 150;
            rotateAnimation.From = 0;
            rotateAnimation.To = 360;
            rotateAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.1));
            rotateAnimation.RepeatBehavior = RepeatBehavior.Forever;
            myStoryBoard.Children.Add(rotateAnimation);
            Storyboard.SetTarget(rotateAnimation, canRotate);
            Storyboard.SetTargetProperty(rotateAnimation, new PropertyPath(RotateTransform.AngleProperty));
            
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            canRotate.BeginAnimation(RotateTransform.AngleProperty, rotateAnimation);
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            double saveNum = canRotate.Angle;
            canRotate.BeginAnimation(RotateTransform.AngleProperty, null);
            canRotate.Angle = saveNum;
        }
    }
}
